from peewee import *


db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class UniversityScoreRankMeanRate3(Model):
    id = PrimaryKeyField()
    year = IntegerField()
    score = IntegerField()
    submit_rate = IntegerField()
    batch = IntegerField()
    type = IntegerField()
    code = CharField(max_length=32)
    name = CharField(max_length=64)
    type_name = CharField(max_length=16)
    university_rank = IntegerField()
    mean_lowest_score = IntegerField()
    mean_lowest_rank = IntegerField()
    mean_mean_score = IntegerField()
    mean_mean_rank = IntegerField()
    assign_mean_rank = IntegerField()
    assign_lowest_rank = IntegerField()
    plan_number = IntegerField()
    submit_number = IntegerField()
    enter_number = IntegerField()
    province = CharField(max_length=16)
    city = CharField(max_length=16)
    province_code = IntegerField()
    city_code = IntegerField()
    nature_name = IntegerField()
    other_type = IntegerField()
    dual_class_name = CharField(max_length=32)
    dual_class_A = IntegerField()
    dual_class_B = IntegerField()
    dual_class_subject = IntegerField()
    f985 = IntegerField()
    f211 = IntegerField()
    change_difficult_level = CharField(max_length=32)
    is_deleted = IntegerField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_score_rank_mean_rate_3'  # 文科概率表


    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return UniversityScoreRankMeanRate3.insert(row).execute()
        except Exception as e:
            return ''


    # 批量插入数据
    def toInsertBatch(self, rows):
        try:
            with db.atomic():
                UniversityScoreRankMeanRate3.insert_many(rows).execute()
        except Exception as e:
            print(f'批量写入错误{e}')
            return ''


    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return UniversityScoreRankMeanRate3.select().where(UniversityScoreRankMeanRate3.id == id).get()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getList(self, year):
        try:
            return UniversityScoreRankMeanRate3.select().where(UniversityScoreRankMeanRate3.year == year).dicts().execute()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getOneByYearBatch(self, year, batch, type, code):
        try:
            return UniversityScoreRankMeanRate3.select().where((UniversityScoreRankMeanRate3.year == year) &
                                                      (UniversityScoreRankMeanRate3.batch == batch) &
                                                      (UniversityScoreRankMeanRate3.type == type) &
                                                      (UniversityScoreRankMeanRate3.code == code)).dicts().get()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getCount(self, year, batch, type, code):
        try:
            return UniversityScoreRankMeanRate3.select().where((UniversityScoreRankMeanRate3.year == year) &
                                                      (UniversityScoreRankMeanRate3.batch == batch) &
                                                      (UniversityScoreRankMeanRate3.type == type) &
                                                      (UniversityScoreRankMeanRate3.code == code)).count()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdate(self, row, id):
        try:
            return UniversityScoreRankMeanRate3.update(
                year=row['year'], batch=row['batch'],
                type=row['type'], code=row['code'], name=row['name'], type_name=row['type_name'], university_rank=row['university_rank'],
                mean_lowest_score=row['mean_lowest_score'], mean_lowest_rank=row['mean_lowest_rank'],
                mean_mean_score=row['mean_mean_score'], mean_mean_rank=row['mean_mean_rank'],
                assign_mean_rank=row['assign_mean_rank'], assign_lowest_rank=row['assign_lowest_rank'],
                plan_number=row['plan_number'], submit_number=row['submit_number'], enter_number=row['enter_number'],
                province=row['province'], city=row['city'], province_code=row['province_code'], city_code=row['city_code'],
                nature_name=row['nature_name'], f211=row['f211'], f985=row['f985'], dual_class_name=row['dual_class_name'],
                dual_class_A=row['dual_class_A'], dual_class_B=row['dual_class_B'], dual_class_subject=row['dual_class_subject']
            ).where(UniversityScoreRankMeanRate3.id == id).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateName(self, row):
        try:
            return UniversityScoreRankMeanRate3.update(name=row['name']).where(UniversityScoreRankMeanRate3.id == row['id']).execute()
        except Exception as e:
            return ''

    # 更新单条数据(返回影响记录数)
    def toUpdateAddress(self, row):
        try:
            return UniversityScoreRankMeanRate3.update(province_code=row['province_code'], province=row['province']).where(
                UniversityScoreRankMeanRate3.id == row['id']).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateAddress2(self, row):
        try:
            return UniversityScoreRankMeanRate3.update(city_code=row['city_code'],
                                                  city=row['city']).where(
                UniversityScoreRankMeanRate3.id == row['id']).execute()
        except Exception as e:
            return ''


if __name__ == '__main__':
    universityScoreRankMean = UniversityScoreRankMeanRate3()
    count = universityScoreRankMean.toUpdate(dict(), 1917)
    if count:
        print(count)
    else:
        print("空")